#include "pch.h"
#include "MainWindow.h"
#include "Application.h"
#include "LoggerModel.hpp"

#ifdef _DEBUG
	#include <mld/mld.hpp>
#endif




MainWindow::MainWindow(services::provider::const_ptr provider, Bot::Ptr bot)
	: m_bot(bot)
	, m_plugins(m_bot->plugins())
{
	ui.setupUi(this);
	LoggerModel* loggerModel = new LoggerModel(ui.logTableView, provider);
	ui.logTableView->setModel(loggerModel);

	ui.activeRequestList->setModel(&m_activeRequests);
	ui.pluginList->setModel(&m_pluginModel);

	QStringList plugins;
	std::for_each(m_plugins.begin(), m_plugins.end(), [&](PluginLoader::Ptr plugin)
	{
		std::for_each(plugin->begin(), plugin->end(), [&](Plugin::Ptr interface)
		{
			plugins << QString( "%1 (%2)" ).arg(interface->name()).arg(interface->version());
		});
	});
	m_pluginModel.setStringList(plugins);

	connect(loggerModel, SIGNAL(rowsInserted(const QModelIndex&, int, int)), this, SLOT(updateLog()));
	connect(&m_timer, SIGNAL(timeout()), this, SLOT(onUpdate()));
	connect(ui.pluginList, SIGNAL(clicked(const QModelIndex &)), this, SLOT(onPluginClick(const QModelIndex &)));

	m_timer.start( 50 );
}
///////////////////////////////////////////////////////////////////////////////////////////////////

MainWindow::~MainWindow()
{}
///////////////////////////////////////////////////////////////////////////////////////////////////




void MainWindow::onUpdate()
{
	QTime rt( 0, 0, 0, 0 );
	rt = rt.addMSecs( m_bot->startTime().msecsTo( QTime::currentTime() ) );

	ui.runtime->setText( rt.toString( "hh:mm:ss" ) );
	ui.currentRequests->setText( QString( "%1/s" ).arg( m_bot->requestRate() ) );
	ui.activeRequests->setText( QString::number( m_bot->numActiveRequests() ) );
	ui.pendingRequests->setText( QString::number( m_bot->numPendingUrls() ) );
	ui.completedRequests->setText( QString::number( m_bot->numCompletedRequests() ) );
	ui.filteredReplies->setText( QString::number( m_bot->numFilteredReplies() ) );
	ui.currentUpload->setText( QString( "%1 kb/s" ).arg( m_bot->uploadRate() ) );
	ui.currentDownload->setText( QString( "%1 kb/s" ).arg( m_bot->downloadRate() ) );
	ui.filteredURLs->setText( QString::number( m_bot->numFilteredUrls() ) );


	QStringList list;
	foreach(const QUrl &url, m_bot->activeRequests())
	{
		list << url.toString();
	}
	m_activeRequests.setStringList( list );
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void MainWindow::updateLog()
{
	ui.logTableView->resizeColumnsToContents();
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void MainWindow::onPluginClick(const QModelIndex &index)
{
	/*QList<PluginPtr> plugins = Application::instance()->bot().plugins();
	if( index.row() < plugins.size() )
	{
		const PluginPtr &plugin = plugins[ index.row() ];
		pluginDescription->setText( plugin->description() );

		pluginEnabled->setChecked( plugin->enabled() );
	}*/
}
///////////////////////////////////////////////////////////////////////////////////////////////////
